package com.jxl.domain.service.impl;

import com.jxl.common.enums.DeletedCodeEnum;
import com.jxl.domain.convert.SubjectCategoryConvert;
import com.jxl.domain.convert.SubjectLabelConvert;
import com.jxl.domain.convert.SubjectMappingConvert;
import com.jxl.domain.entity.SubjectCategoryBO;
import com.jxl.domain.entity.SubjectLabelBO;
import com.jxl.domain.entity.SubjectMappingBO;
import com.jxl.domain.service.SubjectLabelDomainService;
import com.jxl.infra.basic.entity.SubjectCategory;
import com.jxl.infra.basic.entity.SubjectLabel;
import com.jxl.infra.basic.entity.SubjectMapping;
import com.jxl.infra.basic.service.SubjectLabelService;
import com.jxl.infra.basic.service.SubjectMappingService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    @Resource
    private SubjectLabelService subjectLabelService;

    @Resource
    private SubjectMappingService subjectMappingService;



    @Override
    public void add(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel =
                SubjectLabelConvert.INSTANCE.convertBoToSubjectLabel(subjectLabelBO);
        subjectLabelService.insert(subjectLabel);
    }



    @Override
    public Boolean update(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel =
                SubjectLabelConvert.INSTANCE.convertBoToSubjectLabel(subjectLabelBO);
        Integer update = subjectLabelService.update(subjectLabel);
        return update>0;
    }

    @Override
    public Boolean delete(Long id) {
        SubjectLabel subjectLabel = new SubjectLabel();
        subjectLabel.setId(id);
        subjectLabel.setIsDeleted(DeletedCodeEnum.DELETE.getCode());
        Integer update = subjectLabelService.update(subjectLabel);
        return update>0;
    }

    @Override
    public List<SubjectLabelBO> queryByCategory(SubjectLabelBO subjectLabelBO) {
        //查询出标签id的集合
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setIsDeleted(DeletedCodeEnum.UN_DELETE.getCode());
        subjectMapping.setCategoryId(subjectLabelBO.getCategoryId());
        List<SubjectMapping> list = subjectMappingService.queryLabelByCategoryIds(subjectMapping);
        if (CollectionUtils.isEmpty(list)) return null;
        List<Long> labelIds = list.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
        //再查询出标签的所有信息
        List<SubjectLabel> labelList = subjectLabelService.queryByIds(labelIds);
        //然后再进行组装
        List<SubjectLabelBO> labelBOS = new ArrayList<>();
        labelList.forEach(label->{
            SubjectLabelBO subjectLabelBO1 = new SubjectLabelBO();
            subjectLabelBO1.setId(label.getId());
            subjectLabelBO1.setLabelName(label.getLabelName());
            subjectLabelBO1.setSortNum(label.getSortNum());
            subjectLabelBO1.setCategoryId(subjectLabelBO.getCategoryId());
            subjectLabelBO1.setIsDeleted(label.getIsDeleted());
            labelBOS.add(subjectLabelBO1);
        });
        return labelBOS;
    }


}
